package com.softtown.modules.workflow.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.enums.SwfPhaseNameEnum;
import com.softtown.modules.workflow.dao.SwfPhaseNameDefDao;
import com.softtown.modules.workflow.dto.SwfPhaseNameDefDTO;
import com.softtown.modules.workflow.dto.SwfPhaseNameDefPageDTO;
import com.softtown.modules.workflow.entity.SwfPhaseNameDefEntity;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.security.user.UserDetail;
import com.softtown.modules.workflow.service.SwfPhaseNameDefService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/8/22 13:53
 * @Description:
 */
@Service
public class SwfPhaseNameDefServiceImpl extends BaseServiceImpl<SwfPhaseNameDefDao, SwfPhaseNameDefEntity> implements SwfPhaseNameDefService {

    /**
     * 通过id找出审批段名字
     *
     * @param id id
     * @return com.softtown.modules.entity.SwfPhaseNameDefEntity
     * @Author Dingyayong
     * @Date 2020/4/13 19:37
     */
    @Override
    public SwfPhaseNameDefEntity getSwfPhaseNameById(Long id) {
        return baseDao.getSwfPhaseNameById(id);
    }

    @Override
    public List<SwfPhaseNameDefEntity> getSwfPhaseNameByName(String name, Long compId) {
        return baseDao.getSwfPhaseNameByName(name,compId);
    }

    /**
     * 通过公司id找出审批段名字List
     *
     * @param swfPhaseNameDefPageDTO 公司审批段定义DTO
     * @return com.github.pagehelper.PageInfo<com.softtown.modules.entity.SwfPhaseNameDefEntity>
     * @Author Dingyayong
     * @Date 2020/4/13 19:38
     */
    @Override
    public PageInfo<SwfPhaseNameDefEntity> getPhaseNameDefList(SwfPhaseNameDefPageDTO swfPhaseNameDefPageDTO) {
        Integer pageSize = swfPhaseNameDefPageDTO.getPageSize();
        Integer page = swfPhaseNameDefPageDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<SwfPhaseNameDefEntity> swfPhaseNameDefEntities = baseDao.getPhaseNameDefList(swfPhaseNameDefPageDTO.getCompId());
        return new PageInfo<>(swfPhaseNameDefEntities);
    }

    /**
     * 通过公司id找出审批段名字List
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return java.util.List<com.softtown.modules.entity.SwfPhaseNameDefEntity>
     * @Author Dingyayong
     * @Date 2020/4/13 19:39
     */
    @Override
    public List<SwfPhaseNameDefEntity> getAprvNameList(SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        return baseDao.getPhaseNameDefList(swfPhaseNameDefDTO.getCompId());
    }

    @Override
    public List<SwfPhaseNameDefEntity> getAprvNameByStatusList(
        SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        return baseDao.getAprvNameByStatusList(swfPhaseNameDefDTO.getCompId());
    }

    /**
     * 添加公司审批段名字
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 19:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPhaseNameDef(SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        SwfPhaseNameDefEntity swfPhaseNameDefEntity = new SwfPhaseNameDefEntity();
        swfPhaseNameDefEntity.setCompId(swfPhaseNameDefDTO.getCompId());
        swfPhaseNameDefEntity.setName(swfPhaseNameDefDTO.getName());
        swfPhaseNameDefEntity.setSort(swfPhaseNameDefDTO.getSort());
        swfPhaseNameDefEntity.setRemark(swfPhaseNameDefDTO.getRemark());
        swfPhaseNameDefEntity.setStatus(SwfPhaseNameEnum.NAME_ENUM_EN.value());
        baseDao.insert(swfPhaseNameDefEntity);
    }

    /**
     * 激活/停用审批段名称定义
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 19:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void activePhaseNameDef(SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        UserDetail userDetail = SecurityUser.getUser();
        SwfPhaseNameDefEntity swfPhaseNameDefEntity = baseDao.getSwfPhaseNameById(swfPhaseNameDefDTO.getId());
        if (swfPhaseNameDefEntity.getStatus().equals(SwfPhaseNameEnum.NAME_ENUM_DIS.value())) {
            swfPhaseNameDefEntity.setStatus(SwfPhaseNameEnum.NAME_ENUM_EN.value());
            swfPhaseNameDefEntity.setIsDeleted(SwfPhaseNameEnum.NAME_ENUM_DIS.value());
            swfPhaseNameDefEntity.setUpdateDate(new Date());
            swfPhaseNameDefEntity.setUpdater(userDetail.getId());
            swfPhaseNameDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            baseDao.updateSwfPhaseName(swfPhaseNameDefEntity);
        } else {
            swfPhaseNameDefEntity.setStatus(SwfPhaseNameEnum.NAME_ENUM_DIS.value());
            swfPhaseNameDefEntity.setIsDeleted(SwfPhaseNameEnum.NAME_ENUM_DIS.value());
            swfPhaseNameDefEntity.setUpdateDate(new Date());
            swfPhaseNameDefEntity.setUpdater(userDetail.getId());
            swfPhaseNameDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            baseDao.updateSwfPhaseName(swfPhaseNameDefEntity);
        }
    }

    /**
     * 编辑审批段名称定义
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 19:42
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePhaseNameDef(SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        UserDetail userDetail = SecurityUser.getUser();
        SwfPhaseNameDefEntity swfPhaseNameDefEntity = baseDao.getSwfPhaseNameById(swfPhaseNameDefDTO.getId());
        swfPhaseNameDefEntity.setName(swfPhaseNameDefDTO.getName());
        swfPhaseNameDefEntity.setSort(swfPhaseNameDefDTO.getSort());
        swfPhaseNameDefEntity.setRemark(swfPhaseNameDefDTO.getRemark());
        swfPhaseNameDefEntity.setIsDeleted(SwfPhaseNameEnum.NAME_ENUM_DIS.value());
        swfPhaseNameDefEntity.setUpdateDate(new Date());
        swfPhaseNameDefEntity.setUpdater(userDetail.getId());
        swfPhaseNameDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        baseDao.updateSwfPhaseName(swfPhaseNameDefEntity);
    }

    /**
     * 删除审批段名称定义
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 19:43
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePhaseNameDef(SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        UserDetail userDetail = SecurityUser.getUser();
        SwfPhaseNameDefEntity swfPhaseNameDefEntity = baseDao.getSwfPhaseNameById(swfPhaseNameDefDTO.getId());
        swfPhaseNameDefEntity.setIsDeleted(null);
        swfPhaseNameDefEntity.setUpdateDate(new Date());
        swfPhaseNameDefEntity.setUpdater(userDetail.getId());
        swfPhaseNameDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        baseDao.updateSwfPhaseName(swfPhaseNameDefEntity);
    }
}
